﻿using System;
using System.Collections.Generic;
using DISP.Core;
using Ninject;

namespace DISP.Wrappers.Ninject
{
    internal class NinjectWrapper : IIoCContainerWrapper
    {
        private IKernel Kernel { get; set; }

        public NinjectWrapper(IEnumerable<DependencyInjectionService> services)
        {
            this.Kernel = new StandardKernel();

            foreach (var service in services)
            {
                if (!service.SourceType.IsAssignableFrom(service.TargetType))
                {
                    throw new InvalidOperationException("TargetType is not an implementation of SourceType");
                }
                switch (service.Scope)
                {
                    case DependencyInjectionScopeEnum.Singleton:
                        this.Kernel.Bind(service.SourceType).To(service.TargetType).InSingletonScope();
                        break;
                    case DependencyInjectionScopeEnum.Thread:
                        this.Kernel.Bind(service.SourceType).To(service.TargetType).InThreadScope();
                        break;
                    case DependencyInjectionScopeEnum.Transient:
                        this.Kernel.Bind(service.SourceType).To(service.TargetType).InTransientScope();
                        break;
                    case DependencyInjectionScopeEnum.None:
                        this.Kernel.Bind(service.SourceType).To(service.TargetType);
                        break;
                }
            }
        }

        public void BindService(DependencyInjectionService service)
        {
            throw new NotImplementedException();
        }

        public TService GetService<TService>()
        {
            return (TService)GetService(typeof(TService));
        }

        public Object GetService(Type service)
        {
            return this.Kernel.GetService(service);
        }
    }
}
